import axios from 'axios'
import qs from 'qs'

// 创建一个包含默认配置的axios实例
const axiosInstance = axios.create({
  withCredentials: true, // 确保跨域请求时携带cookie
  headers: {
    'Content-Type': 'application/json',
    'X-Requested-With': 'XMLHttpRequest'
  }
})

/** 创建订单 */
export const createOrder = (params: object) => {
    return axiosInstance.post('/api/order/create.do', qs.stringify(params), {
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    })
}
/** 支付订单 */
export const payOrder = (params: object) => {
    return axiosInstance.post('/api/order/pay.do', qs.stringify(params), {
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    })
}
/** 取消订单 */
export const cancelOrder = (orderId: string) => {
    return axiosInstance.post('/api/order/cancel.do', qs.stringify({ orderId }), {
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    })
}
/** 获取用户所有订单 */
export const getUserOrders = (userId: string) => {
    return axiosInstance.get('/api/order/user/list.do', { params: { userId } })
}
/** 获取所有订单（管理员） */
export const getOrderList = () => {
    return axiosInstance.get('/api/order/all/list.do')
}
/** 根据酒店名称获取订单 */
export const getOrdersByHotel = (hotelName: string) => {
    return axiosInstance.get('/api/order/hotel/list.do', { params: { hotelName } })
}
/** 根据订单ID查询订单 */
export const getOrderById = (id: number) => {
    return axiosInstance.get('/api/order/get.do', { params: { id } })
}
/** 更新订单信息 */
export const updateOrder = (params: object) => {
    return axiosInstance.post('/api/order/update.do', qs.stringify(params), {
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    })
}
/** 删除订单 */
export const deleteOrder = (id: number) => {
    return axiosInstance.post('/api/order/delete.do', qs.stringify({ id }), {
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    })
}
